package com.eoe.wiki.utils;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;

import android.text.TextUtils;
import android.util.Log;

import com.eoe.wiki.Constants;
import com.eoe.wiki.WikiConfig;


public class L {

	private static final String TAG				= "eoeWiki";
	/**
	 * log file name
	 */
	public static final String  PERSIST_PATH	= Constants.LOGS_DIR + File.separator+ "log";
	
	public static void p(String log){
		if(WikiConfig.isDebug()){
			System.out.println(log);
		}
		L.d(log);
	}
	
	public static void v(String log){
		print(log, Log.VERBOSE);
	}
	
	public static void d(String log){
		print(log, Log.DEBUG);
	}
	
	public static void i(String log){
		print(log, Log.INFO);
	}
	
	public static void w(String log){
		print(log, Log.WARN);
	}
	
	public static void e(String log){
		print(log, Log.ERROR);
	}
	
	public static void e(String log,Throwable throwable){
		print(log + "#message:" + throwable.getMessage(), Log.ERROR);
		StackTraceElement[] elements = throwable.getStackTrace();
		for(StackTraceElement element : elements){
			print(element.toString(), Log.ERROR);
		}
	}
	
	private static synchronized void print(final String log,final int level){
		if(TextUtils.isEmpty(log)){
			return;
		}
		if(WikiConfig.isDebug()){
			switch(level){
			case Log.VERBOSE:
				Log.v(TAG, log);
				break;
			case Log.DEBUG:
				Log.d(TAG, log);
				break;
			case Log.INFO:
				Log.i(TAG, log);
				break;
			case Log.WARN:
				Log.w(TAG, log);
				break;
			case Log.ERROR:
				Log.e(TAG, log);
				break;
			}
		}
		if(WikiConfig.isPersistLog()){
			ThreadPoolUtil.execute(new Runnable() {
				
				@Override
				public void run() {
					// TODO Auto-generated method stub
					writeLog(log, level);
				}
			});
		}
	}
	
	private static synchronized void writeLog(String log,int level){
		StringBuffer sb = new StringBuffer();
		sb.append("[" + DateUtil.toTime(System.currentTimeMillis(),DateUtil.DATE_FORMAT_HOUR_MINUTE_SECOND) + "]");
		switch(level){
		case Log.VERBOSE:
			sb.append("[VERBOSE]\t");
			break;
		case Log.DEBUG:
			sb.append("[DEBUG]\t");
			break;
		case Log.INFO:
			sb.append("[INFO]\t");
			break;
		case Log.WARN:
			sb.append("[WARN]\t");
			break;
		case Log.ERROR:
			sb.append("[ERROR]\t");
			break;
		}
		RandomAccessFile raf = null;
		
		
		FileUtil.initExternalDir(false);
		try {
			String fileName = PERSIST_PATH + "_" + DateUtil.toTime(System.currentTimeMillis(),DateUtil.DATE_DEFAULT_FORMAT);
			File logFile = new File(fileName);
			if(!logFile.exists()){
				logFile.createNewFile();
			}
			raf = new RandomAccessFile(logFile, "rw");
			raf.seek(raf.length());
			raf.writeBytes(sb.toString() + log + "\r\n");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			L.p("can`t write the log tp file");
		}finally{
			if(raf!=null){
				try {
					raf.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
	}
}
